// pages/CardInfo/CardInfo.js
const app = getApp()
const util = require('../../../utils/util.js')
const http = require('../../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    bankNumber: '',
    phone: '',
    hide: true,
    gets: false,
    isShow: false,
    tops: Number(getApp().globalData.statusBarHeight +46)
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      let realname = options.realname;
      let is_cards = wx.getStorageSync('member').member_info.id_card;
      let agent = options.agent;
      this.setData({
        realname: realname,
        id_card: is_cards,
        agent: agent
      })
    }
  },
  //姓名
  getUserIdCardName: function (e) {
    this.setData({
      name: e.detail.value,
    })


  },
  //提交
  submit() {
    let mobile = this.data.phone;
    if (mobile.length == 0) {
      this.toastDialog.showDialog('请输入手机号！');
      return false;
    }
    if (mobile.length != 11) {
      this.toastDialog.showDialog('手机号长度有误！');
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      this.toastDialog.showDialog('手机号长度有误！');
      return false;
    }
    let name = this.data.name;
    if (name) {
      var realname = this.data.name;
    } else {
      var realname = this.data.realname
    }
    var bank_card = this.data.num;
    mobile = mobile;
    var id_card = this.data.id_card;
    const data ={
      realname:realname,
      id_card:id_card,
      bank_card:bank_card,
      mobile:mobile
    }
    this.bindBank(data)
  },
  //手机号
  getPhone(e) {
    let num = this.data.num;
    if (num.length == 0) {
      this.toastDialog.showDialog('请输入合法卡号')
      this.setData({
        phone: '',
      })
    } else {
      this.setData({
        phone: e.detail.value,
      })
    }

  },
  bindblur(e) {
    this.setData({
      gets: false,
    })
    let num = this.data.num;
    if (num) {
    
      this.checkBank({
        bank_card:num
      })
    }
  },

  //银行卡号
  getUserIdCardNumber: function (e) {
    let name = this.data.name;
    let realname = this.data.realname;
    if (realname || name.length > 1) {
      let card = e.detail.value;
      card = card.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ');
      this.setData({
        bankNumber: card
      })
      let cards = card.replace(/\s|\xA0/g, "");
      this.setData({
        num: cards
      })
    } else {
      this.toastDialog.showDialog('姓名不合法')
      this.setData({
        num: '',
        bankNumber: ''
      })
    }

  },
  //检测银行卡号
  checkBank(data) {
    let _this = this
    _this.toasts.showToast({
      type: 'fail',
      title: '加载中',
      duration: 7000,
    })
    http.postRequest('v1/getBankInfo', data,
    function (res) {
     _this.toasts.hideToast({})
      _this.toastDialog.showDialog(res.msg)
      _this.setData({
        bank:res.data,
        hide:false
      })
    },
    function (res) {
      _this.toasts.hideToast({})
      _this.toastDialog.showDialog(res.msg)

    })
   
   
  },

  //绑定银行卡
  bindBank(data) {
    let _this = this
    _this.toasts.showToast({
      type: 'fail',
      title: '加载中',
      duration: 7000,
    })
    http.postRequest('v1/bindBank', data,
    function (res) {
     _this.toasts.hideToast({})
      _this.toastDialog.showDialog(res.msg)
      wx.redirectTo({
        url: '/pages/onNewPages/CardSucc/CardSucc',
      })

    },
    function (res) {
      _this.toasts.hideToast({})
      _this.toastDialog.showDialog(res.msg)

    })
  },
  /**ffff
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.toasts = this.selectComponent('#toasts')
  },
  //

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})